﻿using System;
using System.Windows.Forms;

namespace 物流管理系统
{
    public partial class Receivinginfo : Form
    {
        DBOperation dboperation = new DBOperation();
        public int formtype;
        ReMatch re = new ReMatch();
        public Receivinginfo()
        {
            InitializeComponent();
        }

        private void confirm_Click(object sender, EventArgs e)
        {
            foreach (Control c in Controls)
            {
                if (c is TextBox txt)
                {
                    if (txt.Text == "" &&(!c.Name.Equals("remarkstext")))
                    {
                        MessageBox.Show("有重要项目为空，请检查！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
                if (c is ComboBox cmb)
                {
                    if (cmb.Text == "")
                    {
                        MessageBox.Show("有重要项目为空，请检查！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
            if (!re.idmatch(idtext.Text))
            {
                MessageBox.Show("编号输入非法！！！\n格式为英文和数字组成的5-15位字符串。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (re.specialmatch(remarkstext.Text))
            {
                MessageBox.Show("请勿随意改写备注！！！\n备注中不能包含有某些特殊符号。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            switch (formtype)
            {
                case 0://添加
                    {
                        if (dboperation.exists("select "+idlabel.Text+" from "+Text+"表 where "+idlabel.Text+"='"+idtext.Text+"'", idtext.Text))
                        {
                            MessageBox.Show("此编号已经存在！！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                        else
                        {
                            remarkstext.Text=DateTime.Now.ToString()+"：收货";
                            dboperation.nonquerysql("insert into 收货信息表 VALUES('"+idtext.Text+"','"+consignmentidcombobox.Text+"','"+receviedatepicker.Value+"','"+paydatepicker.Value+"','"+paywaycombobox.Text+"','"+remarkstext.Text+"');");
                        }
                        //更新
                        dboperation.nonquerysql("update 托运信息表 set 托运状态='已收货',备注=concat(备注,'"+DateTime.Now.ToString()+"：收货') where 托运编号='"+consignmentidcombobox.Text+"';");
                        break;
                    }
                case 1://修改
                    {
                        dboperation.nonquerysql("replace into 收货信息表 VALUES('"+idtext.Text+"','"+consignmentidcombobox.Text+"','"+receviedatepicker.Value+"','"+paydatepicker.Value+"','"+paywaycombobox.Text+"','"+remarkstext.Text+"');");
                        break;
                    }
                case 2://查看
                    {
                        Close();
                        break;
                    }
            }
            Close();
        }

        private void Receivinginfo_Load(object sender, EventArgs e)
        {
            switch (formtype)
            {
                case 0://添加
                    {
                        break;
                    }
                case 1://修改
                    {
                        idtext.Enabled=false;
                        break;
                    }
                case 2://查看
                    {
                        foreach (Control c in Controls)
                        {
                            if (!(c is Button))
                            {
                                c.Enabled=false;
                            }
                        }
                        break;
                    }
            }
        }
    }
}
